package com.shopping.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shopping.commons.response.DataGradResp;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.mapper.OrDetailsMapper;
import com.shopping.model.OrDetails;
import com.shopping.model.Order;
import com.shopping.param.OrDetailsParam;
import com.shopping.service.IOrDetailsService;

@Service
public class OrDetailsService implements IOrDetailsService{

	@Autowired
	private OrDetailsMapper orDetailsMapper;
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public MessageResp insert(OrDetails param) {
		MessageResp resp=new MessageResp();
		//TODO:   这里要对param进行一次加工，添加诸如  addrid    updateperson updatetime  delflag 
		//status     这样的一些参数
		Random r=new Random();
		param.setDetaid(r.nextInt(10000)+100000);//   100000-199999  之间的随机数
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		param.setCreatedate(sdf.format(new Date()));
		if(orDetailsMapper.insertSelective(param)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("添加成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("添加失败");
		}
		return resp;
	}

	public MessageResp update(OrDetails param) {
		MessageResp resp=new MessageResp();
		OrDetails entity=orDetailsMapper.selectByPrimaryKey(param.getDetaid());
		if(entity==null){
			//没找到
			resp.setCode("9999");
			resp.setMsg("未找到修改项");
			return resp;
		}
//		entity.setDelflag("1");
//		entity.setUpdatedate(sdf.format(new Date()));
		entity.setOrderid(param.getOrderid());
		entity.setDelflag(param.getDelflag()==null?CommonCode.DELFLAG_NORMAL:param.getDelflag());
		entity.setCommid(param.getCommid());
		//entity.setStatus(param.getStatus()==null?CommonCode.STATUS_ONE:param.getStatus());
		entity.setDetanum(param.getDetanum());
		
		//自动更新   更新的时间  和更新的人
		entity.setUpdatedate(sdf.format(new Date()));
//		if(addressMapper.updateByPrimaryKey(param)>0){  hH会更新掉所有的字段，没有值的就用null覆盖
		if(orDetailsMapper.updateByPrimaryKeySelective(entity)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("修改成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("修改失败");
		}
		return resp;
	}

	public MessageResp delete(Integer id) {
	//  在实现类中写的任何   与 接口无关的方法，都是在Controller中 不可见的
		MessageResp resp=new MessageResp();
			//TODO:如果没有这个id怎么办
			OrDetails entity=orDetailsMapper.selectByPrimaryKey(id);
			if(entity==null){
				//没找到
				resp.setCode("9999");
				resp.setMsg("未找到删除项");
				return resp;
			}
			entity.setDelflag(CommonCode.DELFLAG_DELETE);
			entity.setUpdatedate(sdf.format(new Date()));//asf.pares("2015-10-10 10:10:10")
//			try {
//				Integer.parseInt("1asdfa");
//			} catch (NumberFormatException e) {
//				e.printStackTrace();
//			}
			if(orDetailsMapper.updateByPrimaryKeySelective(entity)>0){
				//成功了
				resp.setCode("0000");
				resp.setMsg("删除成功");
			}else{
				resp.setCode("9999");
				resp.setMsg("删除失败");
			}
			return resp;
	}

	public DataGradResp<OrDetails> select(OrDetailsParam param) {
		DataGradResp<OrDetails> resp=new DataGradResp<OrDetails>();
		//查询     可能有多个，可能是单个
		//如果前端没有传任何参数的话，delflag也要特殊处理下
		param.setDelflag(param.getDelflag()==null?"0":param.getDelflag());
		List<OrDetails> orDetailsList;
		try {
			orDetailsList = orDetailsMapper.select(param);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setCode("9999");
			resp.setMsg("查询失败");
			return resp;
		}
		resp.setCode("0000");
		resp.setMsg("查询成功");
		resp.setRows(orDetailsList);
		//resp.setObj(orDetailsList);
		resp.setTotal(orDetailsMapper.count(CommonCode.DELFLAG_NORMAL));
		return resp;
	}

	public DataGradResp<OrDetails> select(OrDetails param) {
		// TODO Auto-generated method stub
		return null;
	}

	public MessageResp batchDelete(String ids) {
		// TODO Auto-generated method stub
		return null;
	}


}
